import {  getPayMethod, orderPay, getUserInfo } from '../../../../common/api/index'
Page({
    data: {
		activePay: null,
		orderId: null,
		productIds: null,
		totalMoney: null,
		payMethodsData:[],
		fileList: [],//打款凭证
		totalMoney: 0,
    },
    onLoad(options) {
		if(options.totalMoney && options.productIds && options.orderId){
			this.setData({
				orderId: options.orderId,
				productIds: options.productIds,
				totalMoney: options.totalMoney,
			})
			this.getMethodsList()
		}
		if(options.totalMoney){
			this.setData({
				totalMoney: options.totalMoney
			})
		}
    },
	// 上传打款凭证
	afterRead(event) {
		const { file } = event.detail;
		// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
		wx.uploadFile({
			filePath: file.url,
			name: 'files',
			header:{
				'Authorization': wx.getStorageSync('token')
			},
			url: 'https://www.liyacq.cn/api/common/uploadFile',
			success: (res) => {
				let ress = JSON.parse(res.data)
				let path = ress.data[0].browseUrl
				const { fileList = [] } = path;
				fileList.push({ url: path });
				this.setData({ fileList });
			},
			fail: () => {
			  console.log("上传打款凭证失败...");
			}
		})
	},
	// 支付方式
	getMethodsList(){
		let data = {
			productIds: this.data.productIds
		}
		getPayMethod(data).then(res => {
			if(res.code == 200){
				this.setData({
					payMethodsData: res.data
				})
			}
		})
	},
	// 切换支付方式
	onChange(e){
		this.setData({
			activePay: e.detail
		})
	},
	// 立即支付
	toSubmit(){
		if(!this.data.activePay){
			wx.showToast({
				title: '请选择支付方式',
				icon: 'none',
				duration: 2000
			})
			return
		}
		if( this.data.activePay == 'remit' ){// 对公打款
			if( this.data.fileList.length == 0 ){
				wx.showToast({
					title: '请上传打款凭证',
					icon: 'none',
					duration: 2000
				})
			}else{
				let data = {
					orderId: this.data.orderId,
					payMethod: 'remit',
					remitUrl: this.data.fileList[0].url
				}
				orderPay(data).then(res => {
					if(res.code == 200){
						wx.showToast({
							title: '支付成功',
							icon: 'none',
							duration: 2000
						})
						setTimeout(() => {
							wx.navigateTo({
							  url: '/pages/component/classify/paySuccess/index',
							})
						},2000)
					}
				})
			}
		}else if( this.data.activePay == 'wechat' ){// 微信支付
			
		}else if( this.data.activePay == 'balance' ){// 余额支付
			getUserInfo().then(res => {
				if(res.code == 200){
					this.setData({
						userInfo: res.data.user
					})
					if(res.data.user.balance < this.data.totalMoney){
						wx.showToast({
							title: '余额不足',
							icon: 'none',
							duration: 2000
						})
					}else{
						let data = {
							orderId: this.data.orderId,
							payMethod: 'balance'
						}
						orderPay(data).then(res => {
							if(res.code == 200){
								wx.showToast({
									title: '支付成功',
									icon: 'none',
									duration: 2000
								})
								setTimeout(() => {
									wx.navigateTo({
									url: '/pages/component/classify/paySuccess/index',
									})
								},2000)
							}
						})
					}
				}
			})
			
		}
	},
})